package main

import (
	"fmt"
	"time"
)

func createChannel()chan int{
	out:=make(chan int)
	go func() {
		i:=0
		for {
			time.Sleep(time.Millisecond*500)
			out<-i
			i++
		}
	}()
	return out
}

func createWorker()chan int{
	out:=make(chan int)
	go printChannel(out,0)
	return out
}

func printChannel(c chan int,id int){
	for val:=range c{
		time.Sleep(1*time.Second)
		fmt.Println(id,"and",val)
	}
}

func main() {

	var c1,c2 chan int=createChannel(),createChannel()
	worker:=createWorker()
	n:=0
	values:=make([]int,0)
	tm:=time.After(10*time.Second)
	tick := time.Tick(time.Second)
	for{
		var activeWorker chan int
		var activeValue int
		if len(values)>0{
			activeWorker=worker
			activeValue=values[0]
		}
		select {
		case n= <-c1:
			values=append(values,n)
		case n = <-c2:
			values=append(values,n)
		case activeWorker<-activeValue:
			values=values[1:]
		case <-tm:
			fmt.Println("bye")
			return
		case <-tick:
			fmt.Println("length of values",len(values))
		}
	}

}
